from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from cart import ser
from cart import models
# Create your views here.


# 添加购物车
class AddCartView(APIView):
    def post(self, request):
        # 接收参数
        id=int(request.data.get('id'))
        # 获取用户的id
        user_id=int(request.data.get("user"))
        count=int(request.data.get('count'))
        # 查询是否存在该商品
        data = models.CartModel.objects.filter(user_id=user_id, cake_id=id).first()
        if data:
            # 存在，修改数量
            data.count += count
            data.save()
            return Response({"msg":"已存在数量相加","code":200})
        else:
            # 不存在，添加数据
            models.CartModel.objects.create(user_id=user_id, cake_id=id,count=count)
            return Response({"msg":"添加成功","code":200})
# 获取购物车全部商品
class CartView(APIView):
    def get(self, request):
        # 获取用户的购物车数据
        id=request.GET.get('id')
        data = models.CartModel.objects.filter(user_id=id)
        Ser = ser.CartService(instance=data,many=True)
        return Response(Ser.data)

class DelCartView(APIView):
    def post(self, request):
        # 接收参数
        id=int(request.data.get('id'))
        # 获取用户的id
        user_id=int(request.data.get("user"))
        # 删除数据
        models.CartModel.objects.filter(id=id,user_id=user_id).delete()
        return Response({"msg":"删除成功","code":200})

class XzcartView(APIView):
    def post(self, request):
        id=int(request.data.get('id'))
        # 获取用户的id
        user_id=int(request.data.get("user"))
        # 删除数据
        data=models.CartModel.objects.filter(user_id=user_id,id=id).first()
        if data is None:
            return Response({"msg":"购物车为空","code":400})
        data.isshow=not data.isshow
        data.save()
        return Response({"msg":"修改成功","code":200})
# 全选
class AllCartView(APIView):
    def post(self, request):
        # 获取用户的id
        user_id=int(request.data.get("user"))
        selected=request.data.get("selected")
        # 全选
        models.CartModel.objects.filter(user_id=user_id).update(isshow=selected)
        return Response({"msg":"全选","code":200})

class AllFCartView(APIView):
    def post(self, request):
        # 获取用户的id
        user_id=int(request.data.get("user"))
        # 全选
        models.CartModel.objects.filter(user_id=user_id).update(isshow=False)
        return Response({"msg":"全选","code":200})

class AllNumView(APIView):
    def post(self, request):
        # 获取用户的id
        count=0
        user_id=int(request.data.get("user"))
        # 获取所有选中的数据
        data=models.CartModel.objects.filter(user_id=user_id,isshow=True)
        # ��加数量
        for i in data:
            count+=i.count
        return Response({"msg":"��加数量","code":200,"count":count})
class PricecartView(APIView):
    def post(self,request):
        # 获取用户的id
        user_id=int(request.data.get("user"))
        # 获取所有选中的数据
        data=models.CartModel.objects.filter(user_id=user_id,isshow=True)
        # ��加数量
        price=0
        for i in data:
            price+=i.cake.price*i.count
        return Response({"msg":"��加数量","code":200,"price":price})
class NumADDView(APIView):
    def post(self, request):
        # 接收参数
        id=int(request.data.get('id'))
        # 获取用户的id
        user_id=int(request.data.get("user"))
        # 数量加1
        data=models.CartModel.objects.filter(user_id=user_id,id=id).first()
        data.count+=1
        data.save()
        return Response({"msg":"修改成功","code":200})

class NumDecView(APIView):
    def post(self, request):
        # 接收参数
        id=int(request.data.get('id'))
        # 获取用户的id
        user_id=int(request.data.get("user"))
        # 数量减1
        data=models.CartModel.objects.filter(user_id=user_id,id=id).first()
        if data.count>1:
            data.count-=1
            data.save()
        return Response({"msg":"修改成功","code":200})

# 购物车清空
class ClearCartView(APIView):
    def post(self, request):
        # 获取用户的id
        user_id=int(request.data.get("user"))
        # ��空数据
        models.CartModel.objects.filter(user_id=user_id).delete()
        return Response({"msg":"清空成功","code":200})

